<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>PCMS内容管理系统</title>
<script type="text/javascript" src="<?php echo $_tpl['js']; ?>jquery.js"></script>
<script type="text/javascript" src="<?php echo $_tpl['js']; ?>jquery-ui.custom.min.js"></script>
<link type="text/css" href="<?php echo $_tpl['css']; ?>smoothness/jquery-ui.custom.css" rel="stylesheet" />
<link href="<?php echo $_tpl['css']; ?>admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="ckeditor/config.js"></script>
<script type="text/javascript" src="<?php echo $_tpl['js']; ?>json2.js"></script>

<script type="text/javascript">

		$(function()
			{
				$("#setter").click( function(){ $("#dialog").dialog( {width:450,height:450,buttons: {
							"保存": function() {
								$.get( 'add_taglib_define.php', {"tag":$('#preview_argument').val()}, function(data){
									var obj = $.parseJSON(data);
									if( obj.success == 1 )
									{
										var tagobj = $.parseJSON( $('#preview_argument').val() );
										$('#tagdefine_tips').append('<tr id="tr'+obj.id+'"><td height="20" bgcolor="#272727" style="padding-left:40px;"> $'+tagobj.var_name+' = $taglib->'+tagobj.tagtype+'('+JSON.stringify(tagobj.argument)+')</td></tr>');
									}
									else
									{
										alert( '添加失败：' + obj.message );
									}
								} );

							},

							"取消": function() {
								var yes = window.confirm( '确认取消？' );
								if( yes )
								{
									$(this).dialog("close");
								}
							},

							"预览数据": function() {
								$('#preview_data').focus();
								$.get('preview_taglib_data.php',{"tag":$('#preview_argument').val()},function(data){
								var obj = $.parseJSON(data);
								$('#preview_data').val( data );
								});
							}

						}});
						} );

			$('select').change(gen_argument);
			$('input').change(gen_argument);
			$('#preview_argument').mouseover( function(){ $(this).select(); } );
			$('#preview_data').mouseover( function(){ $(this).select(); } );
			});

			function gen_argument()
			{
				var var_name = $('#var_name').val();
				var tagtype = $('#tagtype').val();
				var cate_id = $('#cate_id').val();
				var include_subcate = $('input:checkbox[name=include_subcate]:checked').val() || 0;
				var record_num = $('#record_num').val() || 999999;
				var pagesize = $('#pagesize').val();
				var order_field = $('#order_field').val();
				var asc_desc = $('input:radio[name=asc_desc]:checked').val();
				var only_promoted = $('input:radio[name=only_promoted]:checked').val() || '';
				var tpl_id = $('#id').val();

				var argument = '{"var_name":"'+var_name+'", "tagtype":"'+tagtype+'","argument":{"cate_id":'+cate_id+', "include_subcate":'+include_subcate+', "record_num":'+record_num+', "pagesize":'+pagesize+', "order_field":"'+order_field+'", "asc_desc":"'+asc_desc+'","only_promoted":"'+only_promoted+'"}, "tpl_id":'+tpl_id+'}';

				$('#preview_argument').val( argument );
			}


			function del_tag( tagid)
			{
				var yes = window.confirm( '确认要删除此条标签定义？依赖此标签的功能将失效。' );
				if( yes )
				{
					$.get( 'del_tag_define.php', {"id":tagid}, function(data){
						var obj = $.parseJSON(data);
						$("#tr"+tagid).hide( 'slow' );
					} );
				}
			}

			var wysiwyg = false;
			function switch_wysiwyg()
			{
				if ( wysiwyg )
				{
					$('#switch_wysiwyg').text( '所见即所得模式' );
					wysiwyg = false;
					var instance = CKEDITOR.instances['htmlsrc']
					instance.destroy();
				}
				else
				{
					$('#switch_wysiwyg').text( '正常模式' );
					wysiwyg = true;

					CKEDITOR.replace( 'htmlsrc',
					{
						toolbar : [
					['Source','Undo','Redo','Find','Replace','SelectAll','RemoveFormat'],
					['Bold','Italic','Underline','Strike'],
					['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
					['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
					['Link','Unlink','Anchor'],
					['Image','Table','HorizontalRule','SpecialChar','PageBreak'],
					['Font','FontSize'],
					['TextColor','BGColor'],
					['Maximize', 'ShowBlocks']
					],
						fullPage : false,
						width:	'100%',
						height:480,
						//protectedSource:[ /<\?[\s\S]*?\?>/ig ],
						filebrowserBrowseUrl : '/admin/ckfinder/ckfinder.html',
						filebrowserImageBrowseUrl : '/admin/ckfinder/ckfinder.html?Type=upload',
						filebrowserFlashBrowseUrl : '/admin/ckfinder/ckfinder.html?Type=upload',
						filebrowserUploadUrl : '/admin/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=upload',
						filebrowserImageUploadUrl : '/admin/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=upload',
						filebrowserFlashUploadUrl : '/admin/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=upload'
					});
				}
			}

	function check()
	{
		var item = document.getElementById( "cate_id" );

		if ( item.options[item.selectedIndex].value == 0 )
		{
			alert( "请选择栏目" );
			item.focus();
			return false;
		}


		var item = document.getElementById( "type" );

		if ( item.options[item.selectedIndex].value == 0 )
		{
			alert( "请选择类型" );
			item.focus();
			return false;
		}

		var item = document.getElementById( "htmlfile" );

		if ( item.value == "" )
		{
			alert( "请选择文件" );
			item.focus();
			return false;
		}

		document.getElementById( "submit" ).disabled = true;
		return true;
	}
</script>
<style>
	.margin5{margin-bottom:10px;}
</style>
</head>

<body style="width:90%">

<!--标签定义 对话框开始-->
	<div id="dialog" title="标签定义" style="display:none">
	<table>
		<tr>
			<td><b>基本定义</b></td>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td width="80">变量名：</td>
			<td><input type="text" id="var_name" name="var_name" /> 字母与下划线的组合</td>
			<td></td>
		</tr>
		<tr>
			<td>标签类型：</td>
			<td><select id="tagtype" name="tagtype"><option value="">请选择</option><?php foreach( $taglist as $key=>$value ){?><option value="<?php echo $value['class_name']; ?>"><?php echo $value['memo']; ?></option><?php } ?></select></td>
			<td></td>
		</tr>
		<tr>
			<td colspan="3"><hr /></td>
		</tr>
		<tr>
			<td><b>参数定义</b></td>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td>栏目：</td>
			<td><select id="cate_id" name="cate_id"><option value="-1">请选择</option><?php
		if ( isset( $cate_data ) && is_array( $cate_data ) )
		{
			$cate_id = isset( $cate_id ) ? $cate_id : 0;
			foreach ( $cate_data as $index => $value )
			{
				$selected = '';
				if ( $value['id'] == $cate_id )
				{
					$selected = 'selected';
				}
				echo '<option value="' . $value['id'] . '" ' . $selected . '>' . str_repeat( '&nbsp;&nbsp;', $value['level'] ) . $value['cname'] . '</option>';
			}
		}
		?></select> <input type="checkbox" name="include_subcate" id="include_subcate" value="1" />是否包括子栏目</td>
			<td></td>
		</tr>
		<tr>
			<td>所有记录数：</td>
			<td><input type="text" id="record_num" name="record_num" size="3" /> 留空取出全部记录</td>
			<td></td>
		</tr>
		<tr>
			<td>每页记录数：</td>
			<td><input type="text" id="pagesize" name="pagesize" size="3" value="15"/> </td>
			<td></td>
		</tr>
		<tr>
			<td>排序字段：</td>
			<td><select id="order_field" name="order_field">
				<option value="" selected="selected">请选择</option>
				<option value="create_time">创建日期</option>
				<option value="fake_click">假点击数</option>
				<option value="home_order_num">首页排序值</option>
				<option value="node_order_num">节点页排序值</option>
				<option value="list_order_num">列表页排序值</option>
			</select> <input type="radio" name="asc_desc" id="asc_desc" value="asc"> 升序 <input type="radio" name="asc_desc" id="asc_desc" value="desc" checked> 降序</td>
			<td></td>
		</tr>
		<tr>
			<td>推荐设置</td>
			<td><input type="radio" name="only_promoted" id="only_promoted" value="promoted_home"> 仅取出推荐到首页记录<br />
			<input type="radio" name="only_promoted" id="only_promoted" value="promoted_node"> 仅取出推荐到栏目页记录<br />
			<input type="radio" name="only_promoted" id="only_promoted" value="promoted_list"> 仅取出推荐到列表页记录<br /></td>
			<td></td>
		</tr>
		<tr>
			<td colspan="3"><hr /></td>
		</tr>
		<tr>
			<td><b>预览</b></td>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td>参数预览</td>
			<td><textarea id="preview_argument" rows="8" cols="35"></textarea></td>
			<td></td>
		</tr>
		<tr>
			<td>数据预览</td>
			<td><textarea id="preview_data" rows="8" cols="35">点击按钮“预览数据”显示的执行结果</textarea></td>
			<td></td>
		</tr></table>
	</div>
<!--标签定义 对话框结束-->



<!--表单开始-->
	<center><h3>编辑模板文件</h3></center>
	<table width="100%" border="0" align="center" cellpadding="5" cellspacing="1">
	<form id="apply" method="post" onsubmit="return check()">
	<input type="hidden" id="reference" name="reference" value="<?php echo $reference; ?>">
	<input type="hidden" id="id" name="id" value="<?php echo $id; ?>">
	  <tr>
		<td height="20" bgcolor="#272727">模板文件：<?php echo $cate_name . '->' . $tpl_type_cname; ?></td>
	  </tr>
	   <tr id="tagdefine_tips">
		<td height="20" bgcolor="#272727"><?php if( ! isset( $refer_tpl_id ) ){ ?><input type="button" id="setter" name="set[]"  value="添加标签" class="btn2"/><?php } ?> </td>
	  </tr>
	   <?php
	   foreach( $tpltaglist as $idx=>$tpltag )
	   {
		   break;
	   ?>
	   <tr id="<?php echo 'tr' . $tpltag['id']; ?>" style="word-break:break-all;">
		<td height="20" bgcolor="#272727" style="padding-left:40px;">
		<?php echo ($idx+1) . '. ' . $tpltag['memo'] . ' $' . $tpltag['var_name'] . ' = $taglib->' . $tpltag['class_name'] . '(' . $tpltag['argument'] . ')'; ?>
		<!--<a href="javascript:void(0);" onClick="javascript:edit_tag(<?php echo $tpltag['id']; ?>)" style="text-decoration:underline">编辑</a>--> <a href="javascript:void(0);" onClick="javascript:del_tag(<?php echo $tpltag['id']; ?>)" style="text-decoration:underline">删除</a>
		</td>
	  </tr>
		<?php
	   }
	   ?>
	 <tr>
		<td height="20" bgcolor="#272727">模板代码：<!--<div style="align:right;float:right;"><a href="javascript:void(0);" onClick="switch_wysiwyg();" id="switch_wysiwyg">所见即所得模式</a></div>--></td>
	  </tr>
	 <tr>
		<td align="left" bgcolor="#272727">
		<textarea name="htmlsrc" id="htmlsrc" style="width:100%;height:480px"><?php echo $file_content; ?></textarea></td>
	  </tr>
	  <tr>
		<td height="20" align="center" bgcolor="#000000">
			<table width="40%">
				<tr align="center">
					<td><input class="btn2" type="submit" id="submit" value="提交"></td>
					<td><input class="btn2" type="button" value="返回" onclick="window.location.href='<?php echo $reference; ?>'"></td>
				</tr>
			</table>
		</td>
	  </tr>
	  <tr>
	  <td>帮助: 模板下可以使用的变量有以下几各种<br/>1.标签变量。添加标签 功能会从封装好的标签类中获取<font color="red">数据</font>（数据保存在你定义的变量中）。 <br/>2.配置参数变量（定义在参数列表功能中）。引用方法：$_argument['变量名']，如$_argument['seo_keyword']。<br/>3.php语言本身支持的全局变量。如$_SERVER等</td>
	  </tr>
	</form>
	</table>
<!--表单结束-->

</body>
</html>